package WaitNotify;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UseWaitAndNotifyTwo {
    static final Object  room = new Object();
    static boolean hasCigarette = false;//有没有烟
    static boolean hasTakeout = false;//有没有外卖

    public static void main(String[] args) {
        new Thread(()->{
            synchronized (room) {
                log.debug("有烟没？[{}]",hasCigarette);
                if(!hasCigarette){
                    log.debug("没烟，先歇会！");
                    try {
//                        Thread.sleep(2000);
                        room.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                log.debug("有烟没？[{}]", hasCigarette);
                if (hasCigarette) {
                    log.debug("可以开始干活了");
                }
            }
        },"小南").start();

        new Thread(()->{
            synchronized (room) {
                log.debug("外卖到嘛了？[{}]",hasTakeout);
                if(!hasTakeout){
                    log.debug("没到，先歇会！");
                    try {
//                        Thread.sleep(2000);
                        room.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                log.debug("外卖到嘛了？[{}]", hasTakeout);
                if (hasTakeout) {
                    log.debug("可以开始干活了");
                }
            }
        },"小女").start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(()->{
            synchronized (room) {
                hasTakeout = true;
                log.debug("外卖到了");
                //随机叫醒，可能是小南，也可能是小女
                room.notify();

                //room.notifyAll();
            }
        },"外卖员").start();

    }
}
